<template>
  <!-- 表格卡片 -->
  <a-card size="small" :bordered="false" :hoverable="true" :scroll="{ x: 1500 }">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-row :gutter="16">
          <a-col :span="10"> <a-input v-model:value="queryForm.key" placeholder="关键字查询" /></a-col>
          <a-col :span="5">
            <a-button type="primary" @click="getInitDateFind"><SearchOutlined />查询</a-button></a-col
          >
          <a-col :span="2">
            <a-button @click="getRemase"><ReloadOutlined />重置</a-button></a-col
          >
        </a-row>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OA.ENTERPRISE" :refresh="getInitDate" />
      </div>
    </a-row>
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      :row-key="(record) => record.blinfoid"
      :pagination="false"
      :loading="tableLoading"
      bordered
      @change="handleTableChange"
    >
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'blapplyprojectname'" title="项目名称">
          <div class="smart-table-operate">
            <span style="color: #66a5fd" @click="getAddInstantly(2, record.blinfoid)"> {{ record.blapplyprojectname }}</span>
          </div>
        </template>
        <template v-if="column.dataIndex === 'approvalOpinion'" title="申报状态">
          <div>
            <div class="smart-table-operate">
              <span :style="record.approvalOpinion == '待审核' || record.approvalOpinion == '审核不通过' ? 'color:red' : ''">
                {{ record.approvalOpinion }}</span
              >
            </div>
          </div>
        </template>
        <template v-if="column.dataIndex === 'blapplyprojectapplystatus'" title="审核流程">
          <div>
            <div class="smart-table-operate">
              <span style="color: #66a5fd" v-if="record.approvalOpinion == '待审核'" @click="showModal(record)"> 点击进入审核流程</span>
              <span style="color: #66a5fd" v-if="record.approvalOpinion != '待审核'" @click="getAddInstantly(3, record.blinfoid)">
                点击查看申报记录</span
              >
              <span style="color: #66a5fd" @click="getAddInstantly(4, record.blinfoid)"> 点击查看记录</span>
            </div>
          </div>
        </template>
      </template>
    </a-table>
    <!-- 分页-->
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.limit"
        v-model:current="queryForm.page"
        v-model:pageSize="queryForm.limit"
        :total="total"
        @change="getInitDate"
        @showSizeChange="getInitDate"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
  <!-- 审核流程 -->
  <a-modal v-model:open="open" title="审核" @ok="handleOk" @cancel="handleCancel" width="600px">
    <a-form :model="formState" name="basic" ref="formRef">
      <a-form-item label="请选择审核结果" name="blapplyprojectapplystatus" :rules="[{ required: true, message: '请选择审核结果' }]">
        <a-select v-model:value="formState.blapplyprojectapplystatus" placeholder="请选择审核结果">
          <a-select-option :value="1">审核通过</a-select-option>
          <a-select-option :value="0">审核不通过</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="请输入审核意见" name="blfundopinions">
        <a-textarea v-model:value="formState.blfundopinions" placeholder="请输入审核意见" :rows="4" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, onMounted, defineEmits, toRaw } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { Project } from '../../../../api/project/project';
  import { smartSentry } from '/@/lib/smart-sentry';
  //--------------------------- 表格 列 ---------------------------
  const columns = ref([
    {
      title: '项目名称',
      dataIndex: 'blapplyprojectname',
      align: 'center',
      width: 150,
    },
    {
      title: '项目负责人',
      dataIndex: 'blprojectmanagepeople',
      align: 'center',
    },
    {
      title: '申报状态',
      dataIndex: 'approvalOpinion',
      align: 'center',
    },
    {
      title: '审核流程',
      dataIndex: 'blapplyprojectapplystatus',
      align: 'center',
    },
    {
      title: '项目编号',
      dataIndex: 'blext2',
      align: 'center',
    },
  ]);
  const queryFormState = {
    page: 1,
    limit: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);
  const getInitDateFind = () => {
    message.success('查询成功');
    getInitDate();
  };
  // 表格初始化
  async function getInitDate() {
    try {
      let res = await Project.findApprovals(queryForm);
      console.log(res, '审批');
      tableData.value = res.page.list;
      total.value = res.page.totalCount;
    } catch (error) {
      smartSentry.captureError(error);
    }
  }
  const open = ref(false);
  const blinfoid = ref();
  const Ishow = ref(''); //财务科审核
  const showModal = (record) => {
    open.value = true;
    blinfoid.value = record.blinfoid;
    Ishow.value = record.blapplyprojectapplystatus;
  };
  //提交按钮
  const handleOk = (blinfoid) => {
    onSubmit();
  };
  //点击审核流程
  const formRef = ref();
  const formState = ref({});
  //提交方法
  const onSubmit = () => {
    formRef.value
      .validate()
      .then(() => {
        let queryFormList = toRaw(formState.value);
        queryFormList.blinfoid = blinfoid.value;
        if (Ishow.value == 3) {
          //此判断是为了动态的传是否是副主任还是主任或秘书长
          queryFormList.blassistantopinions = queryFormList.blfundopinions;
          delete queryFormList.blfundopinions;
          getAdd(queryFormList);
        } else if (Ishow.value == 4) {
          queryFormList.bldirectoropinions = queryFormList.blfundopinions;
          delete queryFormList.blfundopinions;
          getAdd(queryFormList);
        } else if (Ishow.value == 5) {
          queryFormList.blsecretaryopinions = queryFormList.blfundopinions;
          delete queryFormList.blfundopinions;
          getAdd(queryFormList);
        }
      })
      .catch((error) => {
        message.warning('请选择审核意见');
      });
  };
  function getAdd(queryFormList) {
    Project.UpdateApprovals(queryFormList)
      .then((res) => {
        message.success('提交成功');
        formRef.value.resetFields();
        open.value = false;
        getInitDate();
      })
      .catch((e) => {
        smartSentry.captureError(e);
      });
  }
  const handleCancel = () => {
    open.value = false;
    formRef.value.resetFields();
  };
  const getRemase = () => {
    message.success('重置成功');
    queryForm.key = '';
    getInitDate();
  };
  //返回立项申请表格页面
  const emit = defineEmits();
  const getAddInstantly = (e, id) => {
    let obj = {
      type: e,
      id: id,
    };
    emit('getBlack', obj);
  };
  onMounted(() => {
    getInitDate();
  });
</script>
